# -*- coding: utf-8 -*-
__author__ = 'vladbranzei'

import os, logging

client = cherrypy_host = cherrypy_port = mongo_port = mongo_host = mongo_user = mongo_pwd = None

def create_connection(host, port):
    from pymongo import MongoClient
    client = MongoClient(host, port)
    client.admin.authenticate(mongo_user, mongo_pwd)
    return client

def set_host_port():
    global cherrypy_host
    global cherrypy_port
    global mongo_host
    global mongo_port
    global mongo_user
    global mongo_pwd

    try:
        cherrypy_host = os.environ['OPENSHIFT_PYTHON_IP']
        cherrypy_port = int(os.environ['OPENSHIFT_PYTHON_PORT'])
        mongo_host = os.environ['OPENSHIFT_MONGODB_DB_HOST']
        mongo_port = int(os.environ['OPENSHIFT_MONGODB_DB_PORT'])
        mongo_user = os.environ['OPENSHIFT_MONGODB_DB_USERNAME']
        mongo_pwd = os.environ['OPENSHIFT_MONGODB_DB_PASSWORD']
        logging.info('cherrypy host port, mongo host port user pwd fetched')
    except KeyError:
        cherrypy_host = '127.0.0.1'
        cherrypy_port = 8099
        mongo_host = '127.0.0.1'
        mongo_port = 27017
        mongo_user = 'admin'
        mongo_pwd = 'admin'

    logging.info('python ip = ' + cherrypy_host + ' port = ' + str(cherrypy_port))
    logging.info('mongo host = ' + mongo_host + ' port = ' + str(mongo_port))